home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / CLGRAPH1.ZIP / CL_GRAPH.H next >
C/C++ Source or Header  |  1993-09-02  |  18KB  |  511 lines

  1. #ifndef _2D_H
  2. #define _2D_H
  3.  
  4. #if  defined(__DLL__)
  5. #  define _EXPORT _export
  6. #else
  7. #  define _EXPORT _CLASSTYPE
  8. #endif
  9.  
  10.  
  11. extern "C" {
  12. #include "windows.h"
  13. }
  14.  
  15.  
  16. #include <stddef.h>
  17. #include <string.h>
  18. #include <math.h>
  19.  
  20. #include "cl_list.cpp"
  21.  
  22. #define  MEM_ERROR           "Unable to Allocate Memory"
  23.  
  24. #define  NAME_SIZE           20
  25. #define  WIN_TITLE_SIZE      100
  26. #define  WIN_PTR1            "Clumsy_Ptr1"
  27. #define  WIN_PTR2            "Clumsy_Ptr2"
  28.  
  29. #define  BLACK               RGB( 0, 0, 0 )              // color black
  30. #define  WHITE               RGB( 0xFF, 0xFF, 0xFF )     // color white
  31. #define  RED                 RGB( 0xFF, 0, 0 )           // color red
  32. #define  GREEN               RGB( 0, 0xFF, 0 )           // color green
  33. #define  BLUE                RGB( 0, 0, 0xFF )           // color blue
  34.  
  35.  
  36. extern const  HBRUSH NO_BRUSH;                           // null brush
  37.  
  38. extern HANDLE hInst;
  39. extern HANDLE hPrevInst;
  40. extern int    cmdShow;
  41. extern LPSTR  cmdLine;
  42.  
  43. extern void * operator new(size_t size);
  44. extern void operator delete(void * obj);
  45.  
  46.  
  47. LRESULT CALLBACK _export WinProc( HWND hWnd, UINT iMessage,
  48.                                   WPARAM wParam, LPARAM lParam );
  49.  
  50.  
  51. typedef char boolean;
  52.  
  53.  
  54. // window object class
  55. class _EXPORT WinObj
  56. {
  57. public:
  58.   // display a message with a stop icon
  59.   virtual
  60.   void        Error( LPSTR str );
  61.  
  62.   // Display a message with an exclamation icon
  63.   virtual
  64.   void        Warning( LPSTR str );
  65.  
  66.   // Display a message box
  67.   virtual
  68.   int         Message( UINT type, LPSTR str );
  69. };
  70.  
  71.  
  72. // class with a cleanup flag
  73. // this class is inherited by any class that needs a flag to check for
  74. // a cleanup before an object fo that class is destroyed.
  75. class _EXPORT CleanUpCheck
  76. {
  77. protected:
  78.   boolean     cleanUpFlag;
  79.  
  80. public:
  81.               CleanUpCheck()                         { cleanUpFlag = FALSE; }
  82.   void        CleanUp( boolean cleanFlag=TRUE )  { cleanUpFlag = cleanFlag; }
  83. };
  84.  
  85.  
  86. // class for defining coordinates of a point in space
  87. // space is 2-dimentional in this version 
  88. class _EXPORT Coord
  89. {
  90. public:
  91.   float       x, y;
  92.  
  93.               Coord();           // default constructor initializes x = y = 0
  94.               Coord( float xCoor, float yCoor );
  95.   Coord       operator+( Coord& p );                  // return(this + p)
  96.   Coord       operator-();                            // return(-this)
  97.   Coord       operator-( Coord& p );                  // return(this - p)
  98.   Coord       operator*( Coord& p );                  // return(this * p)
  99.   Coord       Rot( float angle, Coord& p );           // return(this rotated
  100.                                                       // by angle around p)         
  101.   Coord&      operator+=( Coord& p );                 // this += p
  102.   Coord&      operator-=( Coord& p );                 // this -= p
  103.   Coord&      operator*=( Coord& p );                 // this *= p
  104.   Coord&      RotBy( float angle, Coord& p );         // rotate this by angle
  105.                                                       // around p
  106. };
  107.  
  108.  
  109. // Center is a global coordinate point representing (0,0)
  110. extern Coord Center;
  111.  
  112.  
  113. // abstract class for all graphic objects
  114. class _EXPORT VisualObject : public WinObj
  115. {
  116. public:
  117.   virtual     ~VisualObject()  {};                      // virtual destructor
  118.   // definition of virtual methods that apply to all graphic objects
  119.   virtual
  120.   void        Translate( Coord& transValue ) = 0;
  121.   virtual
  122.   void        Rotate( float angle, Coord& center = Center ) = 0;
  123.   virtual
  124.   void        Scale( Coord& scale, Coord& center = Center) = 0;
  125.   virtual
  126.   void        Draw( HDC dc, float angle = 0 ) = 0;
  127. };
  128.  
  129.  
  130. // define GraphicList as a linked list of visual object references
  131. typedef LinkedList<VisualObject&> GraphicList;
  132.  
  133.  
  134. // class for graphic objects consisting of a collection of graphic objects
  135. class _EXPORT ComplexGraphic : public VisualObject, public CleanUpCheck
  136. {
  137. private:
  138.   GraphicList container;
  139.  
  140. public:
  141.   virtual     ~ComplexGraphic();
  142.   void        AddObject( VisualObject& vObj );                 // adds object
  143.   void        Translate( Coord& transValue );
  144.   void        Rotate( float angle, Coord& center = Center );
  145.   void        Scale( Coord& scale, Coord& center = Center );
  146.   void        Draw( HDC dc, float angle = 0 );
  147. };
  148.  
  149.  
  150. // class for a space system for graphics 
  151. class _EXPORT Space : public ComplexGraphic
  152. {
  153. private:
  154.   long        infinity;                           // not used in this version
  155. };
  156.  
  157.  
  158. // class for storing Windows pens which are used to define the format
  159. // for drawing lines and arcs
  160. class _EXPORT PenObject
  161. {
  162. protected:                                   
  163.   LOGPEN      lp;                           // pen information
  164.  
  165. public:
  166.               PenObject( HPEN penValue );   // create the pen from an already
  167.                                             // existing pen
  168.               PenObject( COLORREF color );  // create a pen from a color
  169.   void        SetPen( HPEN penValue );      // changes the pen to a new one
  170.   void        SetPen( COLORREF color );     // changes the pen to a new color
  171.   COLORREF    GetColor();                   // returns the color of a pen
  172.   int         GetWidth();                   // returns the width of a pen
  173.   UINT        GetStyle();                   // returns the style of a pen
  174.   HPEN        ActivatePen( HDC dc );        // activates the pen on a device
  175.                                             // context
  176.   void        DeactivatePen( HDC dc, HPEN originalPen );
  177.                                             // removes the pen from a device
  178.                                             // context
  179. };
  180.  
  181.  
  182. // class for storing Windows brushes which are used to define the format
  183. // for filling areas
  184. class _EXPORT BrushObject
  185. {
  186. protected:
  187.   LOGBRUSH    lb;
  188. public:
  189.               BrushObject( HBRUSH brushValue );
  190.               BrushObject( COLORREF color );
  191.   void        SetBrush( HBRUSH brushValue );
  192.   void        SetBrush( COLORREF color );
  193.   COLORREF    GetColor();
  194.   int         GetHatch();
  195.   UINT        GetStyle();
  196.   HBRUSH      ActivateBrush( HDC dc );
  197.   void        DeactivateBrush( HDC dc, HPEN originalBrush );
  198. };
  199.  
  200.  
  201. // graphic class for points
  202. class _EXPORT Point : public PenObject, public VisualObject, protected Coord
  203. {
  204. public:
  205.               Point( float x, float y, HPEN pen ); 
  206.               Point( float x, float y, COLORREF color=BLACK );
  207.   void        Translate( Coord& transValue );
  208.   void        Rotate( float angle, Coord& center = Center );
  209.   void        Scale( Coord& scale, Coord& center = Center );
  210.   void        Draw( HDC dc, float angle = 0 );
  211.   Coord&      GetCoord();
  212. };
  213.  
  214.  
  215. // graphic class for lines
  216. class _EXPORT Line : public PenObject, public VisualObject
  217. {
  218. protected:
  219.   Coord       p1, p2;
  220.  
  221. public:
  222.  
  223.               Line( Coord& p1, Coord& p2, COLORREF color=BLACK );
  224.               Line( Coord& p1, Coord& p2, HPEN pen );
  225.   void        Translate( Coord& transValue );
  226.   void        Rotate( float angle, Coord& center = Center );
  227.   void        Scale( Coord& scale, Coord& center = Center );
  228.   void        Draw( HDC dc, float angle = 0 );
  229.   Coord&      GetCoord1();
  230.   Coord&      GetCoord2();
  231. };
  232.  
  233.  
  234. // graphic class for rectangles
  235. class _EXPORT CRectangle : public BrushObject, public Line
  236. {
  237. private:
  238.   void        Init( float angle );
  239.  
  240. protected:
  241.   Coord       p3, p4;
  242.  
  243. public:
  244.               CRectangle( Coord& topLeft, Coord& bottomRight, float angle,
  245.                           HPEN pen, HBRUSH brush=NO_BRUSH);
  246.               CRectangle( Coord& topLeft, Coord& bottomRight, float angle,
  247.                           HPEN pen, COLORREF brushColor );
  248.               CRectangle( Coord& topLeft, Coord& bottomRight, float angle,
  249.                           COLORREF penColor, HBRUSH brush=NO_BRUSH );
  250.               CRectangle( Coord& topLeft, Coord& bottomRight, float angle,
  251.                           COLORREF penColor, COLORREF brushColor );
  252.   void        Translate( Coord& transValue );
  253.   void        Rotate( float angle, Coord& center =